package com.hnichr.ump.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hnichr.ump.business.Constant;
import com.hnichr.ump.common.doc.entity.Goods;
import com.hnichr.ump.common.doc.service.GoodsService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.filestorage.entity.MxpioFileInfo;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/doc/goods/v1/")
@Tag(name = "GoodsController", description = "文印商城商品接口")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;

	@GetMapping("list")
	@Operation(summary = "商品列表", description = "获取商品列表（无分页）", method = "GET")
	public Result<List<Goods>> list(@Parameter(description = "查询参数") Criteria criteria) {
		criteria.addCriterion("validFlag", Operator.EQ, Constant.STATUS_VALID);

		// 处理价格区间查询
		List<Object> criterions = criteria.getCriterions();
		for (Object obj : criterions) {
			if (obj instanceof SimpleCriterion) {
				SimpleCriterion c = (SimpleCriterion) obj;
				if (StrUtil.equals(c.getFieldName(), "minPrice") && StrUtil.isNotBlank(String.valueOf(c.getValue()))) {
					c.setFieldName("price");
					c.setOperator(Operator.GE);
				}
				if (StrUtil.equals(c.getFieldName(), "maxPrice") && StrUtil.isNotBlank(String.valueOf(c.getValue()))) {
					c.setFieldName("price");
					c.setOperator(Operator.LE);
				}
			}
		}
		List<Goods> list = goodsService.list(Goods.class, criteria);
		return Result.OK(list);
	}

	@GetMapping("page")
	@Operation(summary = "商品列表", description = "获取商品列表", method = "GET")
	public Result<Page<Goods>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("validFlag", Operator.EQ, Constant.STATUS_VALID);

		// 处理价格区间查询
		List<Object> criterions = criteria.getCriterions();
		for (Object obj : criterions) {
			if (obj instanceof SimpleCriterion) {
				SimpleCriterion c = (SimpleCriterion) obj;
				if (StrUtil.equals(c.getFieldName(), "minPrice") && StrUtil.isNotBlank(String.valueOf(c.getValue()))) {
					c.setFieldName("price");
					c.setOperator(Operator.GE);
				}
				if (StrUtil.equals(c.getFieldName(), "maxPrice") && StrUtil.isNotBlank(String.valueOf(c.getValue()))) {
					c.setFieldName("price");
					c.setOperator(Operator.LE);
				}
			}
		}
		Page<Goods> list = goodsService.listPage(Goods.class, page, criteria);
		return Result.OK(list);
	}

	@PostMapping("add")
	@Operation(summary = "新增商品", description = "新增商品", method = "POST")
	public Result<Goods> add(@RequestBody Goods goods) {
		goods.setValidFlag(Constant.STATUS_VALID);
		goodsService.save(goods);
		return Result.OK(goods);
	}

	@PutMapping("edit")
	@Operation(summary = "编辑商品", description = "编辑商品", method = "PUT")
	public Result<Goods> edit(@RequestBody Goods goods) {
		goodsService.update(goods);
		return Result.OK(goods);
	}

	@DeleteMapping("remove/{id}")
	@Operation(summary = "根据id删除商品", description = "根据id删除商品", method = "DELETE")
	public Result<Goods> remove(@PathVariable(name = "id", required = true) String id) {
		goodsService.delete(Goods.class, id);
		return Result.OK();
	}

	@PostMapping("upload")
	@Operation(summary = "上传文件", description = "上传文件", method = "POST")
	public Result<String> upload(@RequestParam("file") MultipartFile file) {
		try {
			MxpioFileInfo fileInfo = goodsService.upload(file);
			return Result.OK(fileInfo.getFileNo());
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("上传失败，请稍后再试");
		}
	}

	@GetMapping("search/{key}")
	@Operation(summary = "根据关键字搜索商品", description = "根据关键字搜索商品", method = "GET")
	public Result<List<Goods>> search(@PathVariable(name = "key", required = true) String key) {
		List<Goods> goodsList = goodsService.search(key);
		return Result.OK(goodsList);
	}
}
